package com.github.freewu.commons.http;

import android.nfc.Tag;

import com.github.freewu.mvp.bean.ServerBean;
import com.github.freewu.mvp.bean.ServerListBean;
import com.github.freewu.mvp.mvpInterface.MvpInterface;
import com.github.freewu.mvp.presenter.Presenter;
import com.google.gson.JsonObject;
import com.lqkj.commons.R;

import java.io.IOException;
import java.util.List;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * http数据处理类
 * Created by free on 16-7-19.
 */
public class HttpDataProcess<V extends MvpInterface.ViewInterface> {
    private V mView;

    public HttpDataProcess(V mView) {
        this.mView = mView;
    }

    /**
     * 处理bean
     *
     * @param call
     * @param action
     * @param <T>
     */
    public <T> void doBean(Call<ServerBean<T>> call, final Action<T, ServerBean> action) {
        mView.getStatusController().showLoadingView();

        call.enqueue(new Callback<ServerBean<T>>() {
            @Override
            public void onResponse(Call<ServerBean<T>> call, Response<ServerBean<T>> response) {
                ServerBean<T> serverBean = response.body();

                if (serverBean != null) {
                    if (response.isSuccessful()) {
                        if (serverBean.isStatus()) {
                            if (serverBean.getData()!=null) {
                                action.onResult(serverBean.getData(), serverBean);

                                mView.getStatusController().showNormalView();
                            }else{
                                mView.getStatusController().showErrorView();
                            }
                        } else {
                            if (action.onError(serverBean)) {
                                mView.getStatusController().showErrorView();
                            }else{
                                mView.getStatusController().showNormalView();
                            }
                        }
                    } else {
                        if (response.raw().code()==504){
                            mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_disable));
                            return;
                        }
                        mView.getStatusController().showErrorView();
                    }
                } else {
                    mView.getStatusController().showErrorView();
                }
            }

            @Override
            public void onFailure(Call<ServerBean<T>> call, Throwable t) {
                mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_time_out));
            }
        });
    }

    /**
     * 处理数组结构的bean
     *
     * @param call
     * @param action
     * @param <T>
     */
    public <T> void doListBean(Call<ServerListBean<T>> call, final Action<List<T>, ServerListBean> action) {
        mView.getStatusController().showLoadingView();

        call.enqueue(new Callback<ServerListBean<T>>() {
            @Override
            public void onResponse(Call<ServerListBean<T>> call, Response<ServerListBean<T>> response) {
                ServerListBean<T> serverBean = response.body();

                if (serverBean != null) {
                    if (response.isSuccessful()) {
                        if (serverBean.isStatus()) {
                            if (serverBean.getData()!=null) {
                                action.onResult(serverBean.getData(), serverBean);

                                mView.getStatusController().showNormalView();
                            }else{
                                mView.getStatusController().showErrorView();
                            }
                        } else {
                            if (action.onError(serverBean)) {
                                mView.getStatusController().showErrorView();
                            }else{
                                mView.getStatusController().showNormalView();
                            }
                        }
                    } else {
                        if (response.raw().code()==504){
                            mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_disable));
                            return;
                        }
                        mView.getStatusController().showErrorView();
                    }
                } else {
                    mView.getStatusController().showErrorView();
                }
            }

            @Override
            public void onFailure(Call<ServerListBean<T>> call, Throwable t) {
                mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_time_out));
            }
        });
    }
    /**
     * 处理数组结构的bean
     *
     * @param call
     * @param action
     * @param <T>
     */
    public <T> void doJsonObject(Call<JsonObject> call, final Action<JsonObject, String> action) {
        mView.getStatusController().showLoadingView();

        call.enqueue(new Callback<JsonObject>() {
            @Override
            public void onResponse(Call<JsonObject> call, Response<JsonObject> response) {
                if (response.isSuccessful()){
                    JsonObject jsonObject=response.body();

                    if (jsonObject!=null){
                        action.onResult(jsonObject,response.message());

                        mView.getStatusController().showNormalView();
                    }else{
                        if (action.onError("")) {
                            mView.getStatusController().showErrorView();
                        }else{
                            mView.getStatusController().showNormalView();
                        }
                    }
                }else{
                    if (response.raw().code()==504){
                        mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_disable));
                        return;
                    }
                    mView.getStatusController().showErrorView();
                }
            }

            @Override
            public void onFailure(Call<JsonObject> call, Throwable t) {
                mView.getStatusController().showReloadView(mView.getContext().getString(R.string.network_time_out));
            }
        });
    }

    public static abstract class Action<R, T> {
        public abstract void onResult(R result, T t);

        /**
         *
         * @return 是否显示错误页
         */
        public boolean onError(T t){
            return true;
        }
    }
}
